home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / lpDaemon SRC / Common Sources / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-13  |  6.4 KB  |  281 lines  |  [TEXT/MPS ]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5.     Modifications by Jim Matthews, Dartmouth College, 5/91
  6.     Further Modifications Casper Boon, Murdoch University 4/92
  7.     
  8. */
  9.  
  10. #ifndef THINK_C
  11. #include <OSUtils.h>
  12. #include <Errors.h>
  13. #include <Files.h>
  14. #include <Resources.h>
  15. #include <Memory.h>
  16. #include <ToolUtils.h>
  17. #endif
  18. #include <Folders.h>
  19. #include <GestaltEqu.h>
  20. #include <DNR.h>
  21. #include <Traps.h>
  22.  
  23.  
  24. #define OPENRESOLVER    1L
  25. #define CLOSERESOLVER    2L
  26. #define STRTOADDR        3L
  27. #define    ADDRTOSTR        4L
  28. #define    ENUMCACHE        5L
  29. #define ADDRTONAME        6L
  30. #define    HINFO            7L
  31. #define MXINFO            8L
  32.  
  33. Handle codeHndl = nil;
  34.  
  35. Boolean TrapAvailable(LongWord trap);
  36.  
  37. typedef OSErr (*OSErrProcPtr)(long, ...);
  38. OSErrProcPtr dnr = nil;
  39.  
  40.  
  41. TrapType GetTrapType(LongWord theTrap);
  42. TrapType GetTrapType(LongWord theTrap)
  43. {
  44.     if (BitAnd(theTrap, 0x0800) > 0)
  45.         return(ToolTrap);
  46.     else
  47.         return(OSTrap);
  48.     }
  49.     
  50. Boolean TrapAvailable(LongWord trap)
  51. {
  52. TrapType trapType = ToolTrap;
  53. LongWord numToolBoxTraps;
  54.  
  55.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  56.         numToolBoxTraps = 0x200;
  57.     else
  58.         numToolBoxTraps = 0x400;
  59.  
  60.     trapType = GetTrapType(trap);
  61.     if (trapType == ToolTrap) {
  62.         trap = BitAnd(trap, 0x07FF);
  63.         if (trap >= numToolBoxTraps)
  64.             trap = _Unimplemented;
  65.         }
  66.     return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  67.  
  68. }
  69.  
  70. static void GetSystemFolder(integer *vRefNumP, LongInt *dirIDP)
  71. {
  72.     SysEnvRec info;
  73.     LongInt wdProcID;
  74.     
  75.     SysEnvirons(1, &info);
  76.     if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
  77.         *vRefNumP = 0;
  78.         *dirIDP = 0;
  79.         }
  80.     }
  81.  
  82. void GetCPanelFolder(integer *vRefNumP, LongInt *dirIDP)
  83. {
  84.     Boolean hasFolderMgr = false;
  85.     LongInt feature;
  86.     
  87.     if (TrapAvailable(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  88.     if (!hasFolderMgr) {
  89.         GetSystemFolder(vRefNumP, dirIDP);
  90.         return;
  91.         }
  92.     else {
  93.         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
  94.             *vRefNumP = 0;
  95.             *dirIDP = 0;
  96.             }
  97.         }
  98.     }
  99.     
  100. /* SearchFolderForDNRP is called to search a folder for files that might 
  101.     contain the 'dnrp' resource */
  102. integer SearchFolderForDNRP(LongInt targetType, LongInt targetCreator,
  103.                                                     integer vRefNum, LongInt dirID)
  104. {
  105.     HParamBlockRec fi;
  106.     Str255 filename;
  107.     LongInt refnum;
  108.     
  109.     fi.fileParam.ioCompletion = nil;
  110.     fi.fileParam.ioNamePtr = filename;
  111.     fi.fileParam.ioVRefNum = vRefNum;
  112.     fi.fileParam.ioDirID = dirID;
  113.     fi.fileParam.ioFDirIndex = 1;
  114.     
  115.     while (PBHGetFInfo(&fi, false) == noErr) {
  116.         /* scan system folder for driver resource files of specific type & creator */
  117.         if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
  118.             fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
  119.             /* found the MacTCP driver file? */
  120.             refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
  121.             if (GetIndResource('dnrp', 1) == NULL)
  122.                 CloseResFile(refnum);
  123.             else
  124.                 return refnum;
  125.             }
  126.         /* check next file in system folder */
  127.         fi.fileParam.ioFDirIndex++;
  128.         fi.fileParam.ioDirID = dirID;    /* PBHGetFInfo() clobbers ioDirID */
  129.         }
  130.     return(-1);
  131.     }    
  132.  
  133. /* OpenOurRF is called to open the MacTCP driver resources */
  134. integer OpenOurRF()
  135. {
  136.     integer refnum;
  137.     integer vRefNum;
  138.     LongInt dirID;
  139.     
  140.     /* first search Control Panels for MacTCP 1.1 */
  141.     GetCPanelFolder(&vRefNum, &dirID);
  142.     refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
  143.     if (refnum != -1) return(refnum);
  144.         
  145.     /* next search System Folder for MacTCP 1.0.x */
  146.     GetSystemFolder(&vRefNum, &dirID);
  147.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  148.     if (refnum != -1) return(refnum);
  149.         
  150.     /* finally, search Control Panels for MacTCP 1.0.x */
  151.     GetCPanelFolder(&vRefNum, &dirID);
  152.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  153.     if (refnum != -1) return(refnum);
  154.         
  155.     return -1;
  156.     }    
  157.  
  158.  
  159. OSErr OpenResolver(char *fileName)
  160. {
  161.     short refnum;
  162.     OSErr rc;
  163.     
  164.     if (dnr != nil)
  165.         /* resolver already loaded in */
  166.         return(noErr);
  167.         
  168.     /* open the MacTCP driver to get DNR resources. Search for it based on
  169.        creator & type rather than simply file name */    
  170.     refnum = OpenOurRF();
  171.  
  172.     /* ignore failures since the resource may have been installed in the 
  173.        System file if running on a Mac 512Ke */
  174.        
  175.     /* load in the DNR resource package */
  176.     codeHndl = GetIndResource('dnrp', 1);
  177.     if (codeHndl == nil) {
  178.         /* can't open DNR */
  179.         return(ResError());
  180.         }
  181.     
  182.     DetachResource(codeHndl);
  183.     if (refnum != -1) {
  184.         CloseWD(refnum);
  185.         CloseResFile(refnum);
  186.         }
  187.         
  188.     /* lock the DNR resource since it cannot be reloated while opened */
  189.     HLock(codeHndl);
  190.     dnr = (OSErrProcPtr) *codeHndl;
  191.     
  192.     /* call open resolver */
  193.     rc = (*dnr)(OPENRESOLVER, fileName);
  194.     if (rc != noErr) {
  195.         /* problem with open resolver, flush it */
  196.         HUnlock(codeHndl);
  197.         DisposHandle(codeHndl);
  198.         dnr = nil;
  199.         }
  200.     return(rc);
  201. }
  202.  
  203.  
  204. OSErr CloseResolver()
  205. {
  206.     if (dnr == nil)
  207.         /* resolver not loaded error */
  208.         return(notOpenErr);
  209.         
  210.     /* call close resolver */
  211.     (void) (*dnr)(CLOSERESOLVER);
  212.  
  213.     /* release the DNR resource package */
  214.     HUnlock(codeHndl);
  215.     DisposHandle(codeHndl);
  216.     dnr = nil;
  217.     return(noErr);
  218. }
  219.  
  220. OSErr StrToAddr(char *hostName, struct hostInfo *rtnStruct,
  221.                                             LongInt resultproc, char *userDataPtr)
  222. {
  223.     if (dnr == nil)
  224.         /* resolver not loaded error */
  225.         return(notOpenErr);
  226.         
  227.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  228. }
  229.  
  230.  
  231. OSErr AddrToStr(LongWord addr, char *addrStr)                                    
  232. {
  233.     if (dnr == nil)
  234.         /* resolver not loaded error */
  235.         return(notOpenErr);
  236.         
  237.     (*dnr)(ADDRTOSTR, addr, addrStr);
  238.     return(noErr);
  239. }
  240.     
  241. OSErr EnumCache(LongInt resultproc, char *userDataPtr)
  242. {
  243.     if (dnr == nil)
  244.         /* resolver not loaded error */
  245.         return(notOpenErr);
  246.         
  247.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  248. }
  249.     
  250.     
  251. OSErr AddrToName(LongWord addr, struct hostInfo *rtnStruct,
  252.                                             LongInt resultproc, char *userDataPtr)                                    
  253. {
  254.     if (dnr == nil)
  255.         /* resolver not loaded error */
  256.         return(notOpenErr);
  257.         
  258.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  259. }
  260.  
  261.  
  262. extern OSErr HInfo(char *hostName, struct returnRec *returnRecPtr,
  263.                                             LongInt resultProc, char *userDataPtr)
  264. {
  265.     if (dnr == nil)
  266.         /* resolver not loaded error */
  267.         return(notOpenErr);
  268.         
  269.     return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  270. }
  271.     
  272. extern OSErr MXInfo(char *hostName, struct returnRec *returnRecPtr,
  273.                                             LongInt resultProc, char *userDataPtr)
  274. {
  275.     if (dnr == nil)
  276.         /* resolver not loaded error */
  277.         return(notOpenErr);
  278.         
  279.     return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  280. }
  281.